package cn.edu.gzhu.workOrderManagement.intercepter;

import cn.edu.gzhu.workOrderManagement.constants.MessageConstant;
import cn.edu.gzhu.workOrderManagement.enumeration.UserAuthorityEnum;
import cn.edu.gzhu.workOrderManagement.util.ResponseUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
//只有总部商务、主管和工单处理员还有客户可以上传附件
public class FileInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Claims claims = (Claims) request.getAttribute("claims");
        UserAuthorityEnum userAuthorityEnum =   UserAuthorityEnum.value2Object(claims.get("authority").toString());
        log.info(userAuthorityEnum.getRole(),"{}：正在上传文件");
        if (userAuthorityEnum != UserAuthorityEnum.HEAD_BUSINESS
                &&userAuthorityEnum!=UserAuthorityEnum.BRANCH_SUPERVISOR
                &&userAuthorityEnum!=UserAuthorityEnum.BRANCH_MATERIAL_HANDLER
                &&userAuthorityEnum!=UserAuthorityEnum.BRANCH_WORK_HANDLER
                &&userAuthorityEnum!=UserAuthorityEnum.CLIENT
        ) {
            log.error(userAuthorityEnum.getRole(),"{}：文件上传权限校验失败");
            ResponseUtil.failResponse(response, MessageConstant.PERMISSION_DENIED);
            return false;
        }
        return true;
    }


}
